package com.vue.admin.service.common.exception;

import java.util.HashMap;

/**
 * @program: vue-admin-service
 * @description: 通用返回结果
 * @author: zhangyangyang
 * @create: 2020-04-03 13:44
 **/
public class ResponseEntity extends HashMap<String, Object> {
    /**
     * 默认的服务端错误
     * @return
     */
    public static ResponseEntity error() {
        return ResponseEntity.error(500, "SERVER_EXCEPTION_UNKNOWN_EXCEPTION");
    }

    /**
     * 自定义code错误
     * @param code
     * @param msg
     * @param msgParams
     * @return
     */
    public static ResponseEntity error(int code, String msg, Object... msgParams) {
        ResponseEntity r = new ResponseEntity();
        r.put("code", Integer.valueOf(code));
        r.put("msg", msg);
        r.put("result", msgParams);
        return r;
    }

    public static ResponseEntity error(String msg, Object... msgParams) {
        return ResponseEntity.error(500, msg, msgParams);
    }

    public static ResponseEntity ok() {
        ResponseEntity r = new ResponseEntity();
        r.put("code",20000);
        r.put("msg","success");
        return r;
    }

    public static ResponseEntity ok(HashMap<String, Object> map) {
        ResponseEntity r = new ResponseEntity();
        r.put("code",20000);
        r.put("msg","success");
        r.putAll(map);
        return r;
    }

    public static ResponseEntity ok(String msg, Object msgParams) {
        ResponseEntity r = new ResponseEntity();
        r.put("code",20000);
        r.put("msg", msg);
        r.put("result", msgParams);
        return r;
    }

    public ResponseEntity() {
        this.put("code", Integer.valueOf(0));
    }

    @Override
    public ResponseEntity put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
